const fs = require('fs')
const USER_FILE_PATH = './data/udata.json'
const CLASS_FILE_PATH = './data/classes.json'
/**
 * 查找所有的用户
 */

function findAll(callback) {
    fs.readFile(USER_FILE_PATH, 'utf8', (err, data) => {
        if (err) {
            return callback(err)
        }
        callback(null, JSON.parse(data).users)
    })
}
/**
 * 根据用户id查询其所管理的班级
 */
function findClassByUserId(id, callback) {
    fs.readFile(CLASS_FILE_PATH, 'utf8', (err, data) => {
        if (err) {
            return callback(err)
        }
        data = JSON.parse(data)
        if (id === "001") {
            callback(null, data.classes)
        } else {
            let newArray = data.classes.filter((item, index) => {
                return item.uid === id
            })
            callback(null, newArray)
        }
    })
}

exports.findAll = findAll
exports.findClassByUserId = findClassByUserId